const {BrowserWindow, Menu, ipcMain} = require('electron/main')
const path = require('node:path')
const CreateMainWindow = require('./MainWindow')
const userData = require('./../global/globalUserData')

module.exports = ()=>{
    const win = new BrowserWindow({
        width: 400,
        height: 400,
        webPreferences: {
            preload: path.join(__dirname, './../preload/preload_welcome.js'),
        }
    })

    win.loadFile(path.join(__dirname, './../Page/WelcomePage/index.html')).then(()=>{
        win.setTitle("myIM");
    })


    // 删除原始顶部菜单栏
    const menu = Menu.buildFromTemplate([])
    Menu.setApplicationMenu(menu)

    // 禁用最大化
    // win.maximizable = false

    // 监听来自渲染进程的消息
    ipcMain.on('window-action', (event, data) => {
        switch (data.action) {
            case 'click-register':
                win.loadFile(path.join(__dirname, '../Page/RegisterPage/registerPage.html'));
                break;
            case 'click-login':
                win.loadFile(path.join(__dirname, '../Page/LoginPage/loginPage.html'));
                break;
            case 'register-success':
                win.loadFile(path.join(__dirname, '../Page/RegSuccessfulPage/RegSuccess.html'));
                console.log('Register success:', data);
                userData.setUserData(data.userName, data.userId, data.userToken);
                break;
            case 'open-main-window':
                CreateMainWindow();
                win.close();
                break;
            default:
                break;
        }
    });

    ipcMain.handle('get-user-data', async (event) => {
        return userData.getUserData();
    })
}